package com.battery.service.impl;

import com.battery.dao.Sys_roleDao;
import com.battery.pojo.Sys_role;
import com.battery.service.Sys_roleService;
import com.battery.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class Sys_roleServicelmpl implements Sys_roleService {
    @Autowired
    private Sys_roleDao sys_roleDao;

    @Override
    public Result addSys_role(Sys_role sys_role){
        try {
            if(sys_roleDao.selectSys_roleByID(sys_role.getRole_id())==null)
                sys_roleDao.insertSys_role(sys_role);
            else return Result.error().message("exists sys_role");
        } catch (DuplicateKeyException e){
            return Result.error().message("重复主键"+e.toString());

        } catch (Exception e){
            return Result.error().message("未知错误："+e.toString());
        }
        return Result.ok().message("插入成功");
    }

    @Override
    public Result removeSys_role(int role_id){
        try {
            if(sys_roleDao.selectSys_roleByID(role_id)!=null)
                sys_roleDao.deleteSys_role(role_id);
            else return Result.error().message("No such a role_id");
        } catch (Exception e){
            return Result.error().message("未知错误："+e.toString());
        }
        return Result.ok().message("删除成功");
    }

    @Override
    public Result changeSys_role(Sys_role sys_role){
        try {
            if(sys_roleDao.selectSys_roleByID(sys_role.getRole_id())!=null)
                sys_roleDao.updateSys_role(sys_role);
            else return Result.error().message("No such a role_id");
        } catch (Exception e){
            return Result.error().message("未知错误："+e.toString());
        }
        return Result.ok().message("修改成功");
    }
    @Override
    public Result searchSys_role(int role_id){
        try {
            if(sys_roleDao.selectSys_roleByID(role_id)!=null)
                sys_roleDao.selectSys_roleByID(role_id);
            else return Result.error().message("No such a role_id");
        } catch (Exception e){
            return Result.error().message("未知错误："+e.toString());
        }
        return Result.ok().data("role_id",sys_roleDao.selectSys_roleByID(role_id).getRole_id()).data("role" +
                "_name",sys_roleDao.selectSys_roleByID(role_id).getRole_name()).data("remark",
                sys_roleDao.selectSys_roleByID(role_id).getRemark()).data("authority",
                sys_roleDao.selectSys_roleByID(role_id).getAuthority());
    }

    @Override
    public Result findAllSys_role(){
        try{
            List<Sys_role> sys_role= sys_roleDao.getAllSys_role();
            if (sys_role == null){
                return Result.error().message("查找无结果");
            }
            else {
                return Result.ok().data("data",sys_role);
            }
        }catch (Exception e) {
            return Result.error().message("查找失败：" + e.toString());
        }


    }
    
}
